package com.traffic.between.adaptor;

import java.util.Date;
import org.apache.log4j.Logger;
import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.ShangTongServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;

@ChooseProvider(channel = "stkj")
public class ShangTongAdaptor implements IAdaptor{
	private static Logger log = Logger.getLogger(ShangTongAdaptor.class);

	public void order() {
		// 获取当前OrderBean
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
	
		String productId = orderBean.getProductCode();
		String phoneNo = orderBean.getPhoneNo();
		String cstmOrderNo = orderBean.getIds();
		log.info("between- 尚通科技流量订购接口请求参数productId:" + productId+",phoneNo:"+phoneNo+",cstmOrderNo:"+cstmOrderNo);
		String result = ShangTongServiceImpl.service.order(productId, phoneNo, cstmOrderNo);
		JSONObject json = JSONObject.fromObject(result);

		String code = (String) json.get("respCode");

		if (("0000".equals(code))) {
		
			orderBean.setMessage((String) json.get("respMsg"));
			orderBean.setErrorMsg("订购中");
			orderBean.setStatus("11");
		} else {
			
			orderBean.setMessage((String) json.get("respMsg"));
			orderBean.setErrorMsg((String) json.get("respMsg"));
			orderBean.setStatus("8");
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	
	public void status() {
		OrderBean orderBean = OrderInfoContext.getContext();
	
		String cstmOrderNo = orderBean.getIds();//param.getString("orderId");
		log.info("between -尚通科技流量订购状态查询接口请求参数 cstmOrderNo：" + cstmOrderNo);
		String result = ShangTongServiceImpl.service.orderStatus(cstmOrderNo);
	
		// 如果 订单 提交成功， 提交成功， 查询结 查询结 果为[],表明 该订单 正在 处理
		if ("[]".equals(result)) {
			orderBean.setMessage("订购中");
			orderBean.setErrorMsg("订购中");
			orderBean.setStatus("1");
			BsmOrderResultExecutor.service.setOrder(orderBean);
			return;
		}
		JSONObject json = JSONObject.fromObject(result);

		String code = (String) json.get("respCode");

		if ("0000".equals(code) || "0001".equals(code)) {
			
			orderBean.setMessage((String) json.get("respMsg"));
			orderBean.setErrorMsg((String) json.get("respMsg"));
			orderBean.setStatus("1");
		} else if ("0002".equals(code)) {
			
			orderBean.setMessage((String) json.get("respMsg"));
			orderBean.setErrorMsg((String) json.get("respMsg"));
			orderBean.setStatus("7");
		} else {
			
			orderBean.setMessage((String) json.get("respMsg"));
			orderBean.setErrorMsg((String) json.get("respMsg"));
			orderBean.setStatus("8");
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
}
